package jia.temp;

import java.net.MalformedURLException;
import jia.core.GameStateManager;
import jia.core.InputProcessor;
import jia.editor.processors.EditorInputProcessor;
import jia.editor.EditorKeyListener;
import jia.editor.EditorUi;
import jia.editor.ModuleEditorPanel;
import jia.core.state.BasicGameStateManager;
import jia.game.GenericFlags;
import jia.game.TestGame;
import jia.game.FirstPersonViewport;
import jia.game.Viewport;

import javax.swing.*;
import java.awt.*;
import org.apache.log4j.Logger;

public class EditorTestFrame extends JFrame implements EditorUi {
    private static final Logger log = Logger.getLogger(EditorTestFrame.class);
    private ModuleEditorPanel editPanel;
    private Viewport fpView;
    private InputProcessor inputProc;
    private GameStateManager gameState;
    
    //====================================================
    /**
     */
    public EditorTestFrame() {
        initComponents();
    }
    
    //====================================================
    /**
     */
    private void initComponents() {
        setTitle("Editor Test Frame");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        inputProc = new EditorInputProcessor();
        
        editPanel = new ModuleEditorPanel();
        Container cp = getContentPane();
        cp.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        
        fpView = new FirstPersonViewport();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        cp.add(fpView, gbc);
        
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.weightx = .75;
        gbc.weighty = .75;
        gbc.fill = GridBagConstraints.BOTH;
        cp.add(editPanel, gbc);
        TestGame game = null;
        try {
            game = new TestGame();
        } catch (MalformedURLException ex) {
            if (GenericFlags.errorMessages){
                log.error(ex.getMessage());
            }
        }
        gameState = new BasicGameStateManager();
        gameState.getPlayerState().setCurrentModule(game.getTestModule());
        inputProc.registerGameState(gameState);
        editPanel.updateGameState(gameState);
        fpView.updateGameState(gameState);
        
        addKeyListener(new EditorKeyListener(inputProc, this));
    }
    
    //====================================================
    /**
     *
     */
    public void updateState() {
        editPanel.updateGameState(gameState);
        fpView.updateGameState(gameState);
    }
    
}
